﻿using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour {

	public static PauseMenu pauseMenu;
	public static GameController gc;
	public bool gameover;
	public Nexus playerOneNexus;
	public Nexus playerTwoNexus;
	public GridCell[,] board = new GridCell[4,4];

	public Texture foregroundHealth;
	public Texture backgroundHealth;

	public bool doingSomethingElse;
	public bool isInMenu;

	public Hand playerOneHand;
	public Hand playerTwoHand;

	public Deck playerOneDeck;
	public Deck playerTwoDeck;
	public Graveyard playerOneGraveyard;
	public Graveyard playerTwoGraveyard;

	public bool playerOneTurn;
	public Vector2 buttonSize;
	public Vector2 cardSize;

	public Texture cardBackground;
	public Texture blackBackgroud;

	public Champion[] champions;

	public string lastAction = "Last Action: Player 1's Turn Began.";

	public float animationTime;
	// Use this for initialization
	void Awake () {
		gc = this;
		buttonSize.x = Screen.width/9;
		buttonSize.y = Screen.height/10;
		cardSize.x = Screen.width/20;
		cardSize.y = Screen.height/10;
		playerOneTurn = true;
		startGame();
		animationTime = .5f;
	}

	public void startGame(){
		int i = 0;
		playerOneDeck.shuffle();
		playerTwoDeck.shuffle();
		while (i < 5){
			playerOneDeck.draw();
			playerTwoDeck.draw();
			i++;
		}
		lastAction = "Last Action: Player 1's Turn Began";
	}

	public void startTurn(){
		playerOneTurn = !playerOneTurn;
		if(playerOneTurn){
			playerOneDeck.draw();
			updateLastAction("Player 1's Turn Started");
		}
		else{
			playerTwoDeck.draw();
			updateLastAction("Player 2's Turn Started");
		}
		foreach(Champion champion in champions){
			if(champion!=null){
				champion.numAttacksThisTurn = 0;
				champion.numMovesThisTurn = 0;
				if(champion.numTurnsIgnited > 0){
					champion.health -=5;
					champion.checkIfDead(champion);
					champion.numTurnsIgnited--;
				}
				if(champion.numTurnsExhausted > 0){
					champion.numTurnsExhausted--;
				}
				if(champion.numGhostMoves > 0){
					champion.numGhostMoves = 0;
					champion.numMoves -= 2;
				}
			}
		}
		if(playerOneNexus.numTurnsFortified > 0)
			playerOneNexus.numTurnsFortified--;
		if(playerTwoNexus.numTurnsFortified > 0)
			playerTwoNexus.numTurnsFortified--;
	}

	public void setGridPosition(GridCell gridcell){
		gc.board[(int) gridcell.gridPosition.x, (int) gridcell.gridPosition.y] = gridcell;
	}

	public void gameOver(){
		gameover = true;
		StartCoroutine(waitToEndGame());
	}

	public IEnumerator waitToEndGame(){
		yield return new WaitForSeconds(5f);
		Application.LoadLevel("Start");
	}

	public IEnumerator waitToStopDoingSomethingElse(){
		yield return new WaitForSeconds(.5f);
		doingSomethingElse = false;
	}

	public void updateLastAction(string action){
		lastAction = "Last Action: "+action;
	}

	void OnGUI(){
		if(playerOneTurn){
			if(GUI.Button(new Rect(Screen.width-buttonSize.x-10, Screen.height-buttonSize.y-10, buttonSize.x, buttonSize.y), "End Turn")){
				startTurn();
				//startTurn ();
			}
		}
		if(gameover){
			GUI.Box(new Rect(Screen.width * 2/5,Screen.height * 4/10,Screen.width * 1.5f/5,Screen.height * 3/10), GameController.gc.blackBackgroud);
			GUI.Label(new Rect(Screen.width/2,Screen.height/2,Screen.width * 1/10,Screen.height * 1/20),"Game Over");
			if(playerTwoNexus.Health<=0)
				GUI.Label(new Rect(Screen.width/2-Screen.width*2/10/4,Screen.height/2 + Screen.height*1/20,Screen.width * 4/10,Screen.height * 1/20),"You Won! Congratulations!");
			else
				GUI.Label(new Rect(Screen.width/2-Screen.width*2/10/2.5f,Screen.height/2 + Screen.height*1/20,Screen.width * 4/10,Screen.height * 1/20), "You Lost. Best Luck Next Time!");
		}
	}
}
